#!/bin/sh

####### alnphy.sh ##################
# Handle alignment-phylogenetic job
####################################

source ./sefunctions.sh

chmod a+x *.sh
chmod a+x *.pl

workflow=$1
taxogroup=$2

lfndir="/biomed/user/d/dttung/panam/outputs/$workflow"

echo "====================Start AlnPhy=========================="
echo ""

# Verify if there is the file alnphy_$taxogroup.tar.gz under $lfndir
# If YES, that means other 'alnphy' jobs has done and has results
# Should have .tar.gz because there are some taxo similare
alnphyverify=`./pnls.sh outputs $workflow | grep "${taxogroup}.tar.gz"`
if [ -n "$alnphyverify" ]; then # File has been uploaded
	echo "File ${taxogroup}.tar.gz has been uploaded by other 'alnphy' job"
	exit 0;
fi

echo -ne "Check preprocess.tar.gz ..."
if [ ! -f preprocess.tar.gz ]; then
	echo "File preprocess.tar.gz is not existed"
	exit 1;
fi
echo " done!"
tar -xzf preprocess.tar.gz # Extract to results and Trimmed_Profiles directory

# Check Reference directory
echo -ne "Check Reference-$workflow.tar.gz ..."
if [ ! -f Reference-$workflow.tar.gz ]; then
	echo "File Reference-$workflow.tar.gz is not existed"
	exit 1;
fi
echo " done!"
tar -xzf Reference-$workflow.tar.gz

# because in preprocess is included but not in Reference (we can change that later)
mv Trimmed_Profiles Reference/

echo -ne "Check alnphytool.tar.gz ..."
# Untar/zip alnphytool.tar.gz (this is tool files)
if [ ! -f alnphytool.tar.gz ]; then
	echo "File alnphytool.tar.gz is not existed"
	exit 1;
fi
echo " done!"
tar -xzf alnphytool.tar.gz # Extract alnphytool.tar.gz => 'lib' directory 
chmod a+x lib/*
chmod a+x *.pl
# debug
ls -l lib

echo "Compile hmm ..."
cd lib/hmm
./configure > compilehmm.log
make >> compilehmm.log
cp src/hmmbuild ../
cp src/hmmalign ../
cd ../..
echo "========================================================"
echo "Algning ..."
./align.pl $taxogroup
echo "========================================================"
echo "Tree constructing ..."
./fasttree.pl $taxogroup
echo "========================================================"
echo "Tree rooting  ...."
./rooting.pl $taxogroup
echo "========================================================"
echo "Tree noding ..."
./nodes.pl $taxogroup
echo "========================================================"
echo "Tree parsing ..."
./parsing.pl $taxogroup
echo "========================================================"
echo "Tree clading ..."
./clade.pl $taxogroup
echo "========================================================"
echo "AlnPhy done!!!"

tar -czf alnphy_$taxogroup.tar.gz results/panam_output

# Verify again before uploading
alnphyverify=`./pnls.sh outputs $workflow | grep "${taxogroup}.tar.gz"`
if [ -n "$alnphyverify" ]; then # File has been uploaded
	echo "File ${taxogroup}.tar.gz has been uploaded by other 'alnphy' job"
	exit 0;
fi
# Copy the results to the grid
echo "Copy the results to the grid ..."
CopyToGrid $lfndir alnphy_$taxogroup.tar.gz 0 copypalnphy.log # No replica at the moment
echo "Done!"